google.load("visualization", "1", {packages:["corechart"]});

var infowindow = new google.maps.InfoWindow();
var markerCluster = null;

function focus_to(lat, lng) {
    var circle;

    var pos = new google.maps.LatLng(lat, lng);
    map.setCenter(pos);
    map.setZoom(14);

    // We load the circle
    circle = new google.maps.Circle({
        map:map,
        center:pos,
        fillColor:'#0070E8',
        fillOpacity:0.6,
        strokeColor:'#001A36',
        strokeOpacity:0.8,
        strokeWeight:1,
        clickable:true
    });
    circle.setRadius(150);
    circle.setMap(map);
    $('.map').focus();
}

$(function () {


    var clicked = 0;
    var nadpis = "";

    $('.vypis').html('<= Vyberte si typ udalosti');
    $('.menu > span').mouseover(function () {
        $('.vypis').html($(this).attr('showtype'));

    }).mouseout(function () {
            if (clicked == 0)
                $('.vypis').html('<= Vyberte si typ udalosti');
            if (clicked == 1)
                $('.vypis').html(nadpis);
        });

    $('.menu > span').click(function () {
        $('.vypis').html($(this).attr('showingtype'));
        clicked = 1;
        nadpis = $(this).attr('showingtype');
    });


});

$(function () {
    function clearMarkersFromArray() {
        for (i = 0; i <= markers.length; i++) {
            markers.shift(markers[i]);
        }
        markers.length = 0;
    }


    $('.menu > span').click(function () {
        // len ak nieco v posli je...
        if (markerCluster != null) {
            markerCluster.clearMarkers();
            clearMarkersFromArray();
        }


        var limit = 15;
        var typ = $(this).attr('type');
        var obr = null;
        var url = 'index.php/main/doMapy/' + typ;


        $.getJSON(url, function (data) {
            $.each(data.markers, function (i, marker_g) {
                var i;
                var myLatlng = new google.maps.LatLng(marker_g.latitude, marker_g.longitude);
                var marker = new google.maps.Marker({
                    position:myLatlng,
                    draggable:true,
                    animation:google.maps.Animation.DROP,
                    icon:'css/images/' + marker_g.type_of_report
                });

                markers.push(marker);


                google.maps.event.addListener(marker, 'click', (function (marker, i) {
                    return function () {
                        infowindow.setContent((marker_g.desc).toString());
                        infowindow.open(map, this);

                    }
                })(marker, i));


            });

            markerCluster = new MarkerClusterer(map, markers);
        });

    });

});